import { ExceptionFilter, Catch, ArgumentsHost, HttpException,HttpStatus } from '@nestjs/common';
import { Request, Response } from 'express';
import globalLogger from 'src/middleWare/logger/winston.config'
export function httpException(message:string){
   throw new HttpException(message, HttpStatus.BAD_REQUEST)
}
@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
  catch(exception: HttpException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse<Response>();
    const request = ctx.getRequest<Request>();
    const status = exception.getStatus();
    const exceptionResponse = exception.getResponse() as {
      [key:string] :any
    };
    const logFormat = `\n >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  Request original url: ${request.originalUrl}
  Method: ${request.method}
  IP: ${request.ip}
  Trace Id: 123456
  Status code: ${status}
  Message: ${exceptionResponse.message || exceptionResponse}
  Parmas: ${JSON.stringify(request.params)}
  Query: ${JSON.stringify(request.query)}
  Body: ${JSON.stringify(request.body)} \n  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  \n`;
globalLogger.error(logFormat)
    response
      .status(status)
      .json({
        statusCode: status,
        message: exceptionResponse.message || exceptionResponse,
        error:exceptionResponse.error
      });
  }
}